
/**
 *  初始化值
 *
 */

function initTextarea() {
	if(profilevar.followReply){
		var value=profilevar.followReply.value;
		var type=profilevar.followReply.type;
		$('#followReplySettingArea li[data-type="'+type+'"] a').click();
		switch (type) {
		case 1:
			$("#followReply").val(value);
			break;
		case 2:
			$('#added_reply_tuwen div[id="'+value+'"]').addClass('current');
			break;
		default:
			break;
		}
	
	}
	if(profilevar.reFollowReply){
		var value=profilevar.reFollowReply.value;
		var type=profilevar.reFollowReply.type;
		$('#refollowReplySettingArea li[data-type="'+type+'"] a').click();
		switch (type) {
		case 1:
			$("#reFollowReply").val(value);
			break;
		case 2:
			$('#added_rereply_tuwen div[id="'+value+'"]').addClass('current');
			break;
		default:
			break;
		}
	}
	if(profilevar.defaultReply){
		var value=profilevar.defaultReply.value;
		var type=profilevar.defaultReply.type;
		$('#defaultReplySettingArea li[data-type="'+type+'"] a').click();
		switch (type) {
		case 1:
			$("#defaultReply").val(value);
			break;
		case 2:
			$('#default_reply_tuwen div[id="'+value+'"]').addClass('current');
			break;
		default:
			break;
		}
		
	}
	if(profilevar.locationReply){
		var value=profilevar.locationReply.value;
		var type=profilevar.locationReply.type;
		$('#locationReplyPane li[data-type="'+type+'"] a').click();
		switch (type) {
		case 1:
			$("#locationReply").val(value);
			break;
		case 2:
			$('#loc_reply_tuwen div[id="'+value+'"]').addClass('current');
			break;
		default:
			break;
		}
	}
	$("#messageReply").val(profilevar.messageReply);
	setWordsLimit($("#followReply"));
	setWordsLimit($("#defaultReply"));
	setWordsLimit($("#reFollowReply"));
	setWordsLimit($("#locationReply"));
}
/**
 * 保存重复关注回复内容
 */
function saveReFollowReply(){
	var $div=$('#refollowReplySettingArea');
	var type=$div.find('.selected').data('type');
	var value="";
	switch (type) {
	case 1:
		value=$('#followReply').val();
		break;
	case 2:
		value=$div.find('.current').attr('id');
		break;
	default:
		break;
	}
	if(type==2 && !value){
		alert('选择要回复的消息');
		return ;
	}
	var json="{type:"+type+",value:'"+value+"'}";
	$.ajax({
		url:"../reply/updateReFollowReply.json",
		type:"post",
		dataType:"json",
		data:{attrValue:json},
		success:function(data){
			if(data.success){
				showMessage('修改成功！',MessageType.OK);
			}else{
				showMessage('保存成功！',MessageType.SaveFaild);
			}
		}
	});
	 
}
/**
 * 保存默认消息回复内容
 */
function saveDefaultReply(){
	var $div=$('#defaultReplySettingArea');
	var type=$div.find('.selected').data('type');
	var value="";
	switch (type) {
	case 1:
		value=$('#defaultReply').val();
		break;
	case 2:
		value=$div.find('.current').attr('id');
		break;
	default:
		break;
	}
	if(type==2 && !value){
		alert('选择要回复的消息');
		return ;
	}
	var json="{type:"+type+",value:'"+value+"'}";
	$.ajax({
		url:"../reply/updateDefaultReply.json",
		type:"post",
		dataType:"json",
		data:{attrValue:json},
		success:function(data){
			if(data.success){
				showMessage('修改成功！',MessageType.OK);
			}else{
				showMessage('保存成功！',MessageType.SaveFaild);
			}
		}
	});
	 
}
/**
 * 保存默认消息回复内容
 */
function saveLocationReply(){
	var $div=$('#locationReplyPane');
	var type=$div.find('.selected').data('type');
	var value="";
	switch (type) {
	case 1:
		value=$('#locationReply').val();
		break;
	case 2:
		value=$div.find('.current').attr('id');
		break;
	default:
		break;
	}
	if(type==2 && !value){
		alert('选择要回复的消息');
		return ;
	}
	var json="{type:"+type+",value:'"+value+"'}";
	$.ajax({
		url:"../reply/updateLocationReply.json",
		type:"post",
		dataType:"json",
		data:{attrValue:json},
		success:function(data){
			if(data.success){
				showMessage('修改成功！',MessageType.OK);
			}else{
				showMessage('保存成功！',MessageType.SaveFaild);
			}
		}
	});
	 
}
/**
 * 保存关注回复内容
 */
function saveFollowReply(){
	var $div=$('#followReplySettingArea');
	var type=$div.find('.selected').data('type');
	var value="";
	switch (type) {
	case 1:
		value=$('#followReply').val();
		break;
	case 2:
		value=$div.find('.current').attr('id');
		break;
	default:
		break;
	}
	if(type==2 && !value){
		alert('选择要回复的消息');
		return ;
	}
	var json="{type:"+type+",value:'"+value+"'}";
	$.ajax({
		url:"../reply/updateFollowReply.json",
		type:"post",
		dataType:"json",
		data:{attrValue:json},
		success:function(data){
			if(data.success){
				showMessage('修改成功！',MessageType.OK);
			}else{
				showMessage('保存成功！',MessageType.SaveFaild);
			}
		}
	});
	 
}
 
$(function () {
	
	$(".catalogList ul li").each(function () {
		$(this).click(function () {
			$(".catalogList ul .selected").removeClass("selected");
			$(this).addClass("selected");
			$(".pane").each(function () {
				$(this).hide();
			});
			$($(this).find("a").attr("pane")).show();
			initTextarea();
		});
	});
			// 修改 被添加回复
	$(".updateflyreply").click(function () {
		var msgvalue = "";
		if ($(this).val() == "\u4fdd\u5b58") {
			msgvalue = $("#followReply").val();
		} else {
			$("#followReply").val('');
			return ;
		}

		$.ajax({url:"../reply/updateFollowReply.json?profile.id=" + profilevar.id, type:"post", data:{attrValue:msgvalue}, dataType:"json", success:function (data) {
			if (data.success) {
				showMessage(Message.SaveSuccess,MessageType.OK);
			} else {
				showMessage(Message.SaveFaild+":" + data.message,MessageType.ERROR);
			}
			if (data.attribute != null && data.attribute.profile != null) {
				profilevar = data.attribute.profile;
				initTextarea();
			}
			
		}});
	});
				// 修改 自动回复
	$(".updatemsgreply").click(function () {
		var msgvalue = "";
		if ($(this).val() == "保存") {
			msgvalue = $("#messageReply").val();
		} else {
			$("#messageReply").val('');
			return;
		}
		$.ajax({url:"../manager/updateMessageReply.json?profile.id=" + profilevar.id, type:"post", data:{attrValue:msgvalue}, dataType:"json", success:function (data) {
			if (data.attribute != null && data.attribute.profile != null) {
				profilevar = data.attribute.profile;
				initTextarea();
			}
			if (data.success) {
				showMessage(Message.SaveSuccess,MessageType.OK);
			} else {
				showMessage(Message.SaveFaild+":" + data.message,MessageType.ERROR);
			}
		}});
	});
	$(".saverefollow").click(function () {
		var msgvalue = "";
		if ($(this).val() == "保存") {
			msgvalue = $("#messageReply").val();
		} else {
			$("#messageReply").val('');
			return;
		}
		var field=$(this).attr('field');
		var val=$("#"+field).val();
		var id=profilevar.id;
		updateSingleFiled(id,"Profile",field,val);
	});
	initTextarea();
});

function editKeyword(a) {
	var ul = $(a).parent().parent();
	var text = $($(a).parent().find(".val").get(0)).text();
	$("#context").val(text).keyup();
	$("#newKeywordDialog").dialog({title:"\u7f16\u8f91\u5173\u952e\u5b57", width:450, height:250, modal:true, position:{at:"center"}, buttons:{"\u786e\u5b9a":function () {
		var text = $("#context").val();
		$(a).parent().find(".val").html(text);
		$(this).dialog("close");
	}, "\u53d6\u6d88":function () {
		$(this).dialog("close");
	}}});
}
function saveRule(a) {
	var detailDiv = $(a).parent().parent();
	var ruleName = detailDiv.find(".ruleName").val();
	var keywords = "";
	detailDiv.find(".keywordItems li").each(function () {
		keywords += $(this).find(".val").html() + "#";
	});
	var ruleMsg = detailDiv.find(".ruleMessage").val();
	var ruleId = detailDiv.find(".ruleId").val();
	var msgid=detailDiv.find('.msgs .current').attr("id");
	var type=detailDiv.find('.role_list .selected').attr('type');
	if(type=='message'){
		ruleMsg=msgid;
	}
	$.ajax({url:"../reply/saveRule.json", type:"post", data:{"privateRule.id":ruleId,'privateRule.type':type, "privateRule.name":ruleName, "privateRule.keyword":keywords, "privateRule.context":ruleMsg}, dataType:"json", success:function (data) {
		if (data.success) {
			initRuleList();
			showMessage(Message.SaveSuccess,MessageType.OK);
		}
	}});
}
function delRule(a){
	var detailDiv = $(a).parent().parent();
	var ruleId=detailDiv.find('.ruleId').val();
	if(ruleId!=0){
		$.ajax({
			url:'../reply/deleteRule.json?privateRule.id='+ruleId,
			dataType:'json',
			success:function(data){
				if(data.success){
					detailDiv.parent().remove();
					showMessage(Message.DeleteSuccess,MessageType.OK);
				}else{
					showMessage(Message.DeleteFaild,MessageType.ERROR);
				}
			}
		});
	}else{
		$('#newRule').click();
	}
}
function initRuleList() {
	showLoading('正在加载...');
	$.get("ruleList.json?ts=" + new Date().getTime(), function (html) {
		$("#keywordSettingArea").html(html);
		// 添加规则
		$("#newRule").click(function () {
			if ($("#listItemNew").hasClass("none")) {
				$(this).html("\u653e\u5f03\u6dfb\u52a0");
				$(this).removeClass("btnGrayS").addClass("btnRed");
				$("#listItemNew .ruleName").val("");
				$("#listItemNew  .keywordItems").html("");
				$("#listItemNew  .ruleMessage").html("");
				$("#listItemNew").removeClass("none");
				$("#listItemNew .detail").show();
			} else {
				$("#listItemNew").addClass("none");
				$(this).removeClass("btnRed").addClass("btnGrayS");
				$(this).html("\u6dfb\u52a0\u89c4\u5219");
			}
		});
	 
		// 展开编辑框
		$(".unfold").click(function () {
			var parent = $(this).parentsUntil()[2];
			$(".ruleItemEditing").removeClass("ruleItemEditing");
			$(parent).addClass("ruleItemEditing");
		});
		// 收起编辑框
		$(".pickup").click(function () {
			var parent = $(this).parentsUntil()[2];
			$(parent).removeClass("ruleItemEditing");
		});
		$(".ruleMessage").each(function () {
			$(this).keyup();
		});
		//规则回复内容选项卡
		$('.role_list .chooseMedia').click(function(){
			var $this=$(this);
			$('.role_list  .selected').removeClass('selected');
			$this.addClass('selected');
			$('.role_list .panel').hide();
			$(".role_list #"+$this.attr("pane")).show();
			$this.parent().parent().find('input[name="type"]').val($this.attr('type'));
			
		});
		$(".appmsg").click(function(){
			$(".msgs .current").removeClass("current");
			$(this).addClass('current');
		});
		
		$(".addKeyword").each(function () {
			$(this).click(function () {
				var ul = $(this).parent().parent().find(".keywordItems").get(0);
				$("#context").val("").keyup();
				$("#newKeywordDialog").dialog({title:"\u6dfb\u52a0\u5173\u952e\u5b57", width:450, height:250, modal:true, position:{at:"center"}, buttons:{"\u786e\u5b9a":function () {
					var text = $("#context").val();
					var html = "<li class=\"item float-p\">";
					html += "<label class=\"left\">";
					html += "<input class=\"left\" type=\"checkbox\">";
					html += "<div class=\"val left\">" + text + "</div>";
					html += "</label>";
					html += "<a class=\"keywordEditor oh z c-opr\" onclick=\"editKeyword(this)\" >\u7f16\u8f91</a></li>";
					$(html).appendTo($(ul));
					$(this).dialog("close");
				}, "\u53d6\u6d88":function () {
					$(this).dialog("close");
				}}});
			});
		});
		$(".delKeyword").each(function () {
			$(this).click(function () {
				var ul = $(this).parent().parent().find(".keywordItems").get(0);
				$(ul).find("input[type=\"checkbox\"]").each(function () {
					if ($(this).attr("checked")) {
						$(this).parent().parent().remove();
					}
				});
			});
		});
		hideLoading();
	});
}

